import createVitePlugins from './vitePlugins'
import { defineConfig } from 'vite'
import path from 'path'
import { uniPostcssPlugin } from '@dcloudio/uni-cli-shared'

export default defineConfig({
  plugins: [createVitePlugins()],
  css: {
    postcss: {
      plugins: [
        require('postcss-import')({
          resolve(id: string) {
            if (id.startsWith('~@/')) {
              return path.resolve(process.env.UNI_INPUT_DIR as string, id.substr(3))
            } else if (id.startsWith('@/')) {
              return path.resolve(process.env.UNI_INPUT_DIR as string, id.substr(2))
            } else if (id.startsWith('/') && !id.startsWith('//')) {
              return path.resolve(process.env.UNI_INPUT_DIR as string, id.substr(1))
            }
            return id
          },
        }),

        uniPostcssPlugin(),
        require('tailwindcss')({ config: './tailwind.config.js' }),
        /* ******* 引入tailwindcss ******* */
        // // 根据平台差异进行不同的样式处理
        ...(process.env.UNI_PLATFORM !== 'h5'
          ? [
              require('postcss-class-rename-postcss8')({
                // 小程序不识别通配符,改成其他类名也会报错所以直接取消掉
                '\\*': 'view',
              }),
            ]
          : [
              require('autoprefixer')({
                remove: true,
              }),
            ]),
      ],
    },
  },
})
